import 'package:dio/dio.dart';
import 'package:flutter_easyrefresh/ball_pulse_footer.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';

import 'model/money_flow_entity.dart';

class GoldFlow extends StatefulWidget {
  @override
  _GoldFlowState createState() => _GoldFlowState();
}

class _GoldFlowState extends State<GoldFlow> {
  MyPageState ps = MyPageState.LOADING;

  MoneyFlowEntity moneyFlowEntity = null;
  EasyRefreshController _easyRefreshController = EasyRefreshController();
  int currentPage = 1;
  int totlePage = 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getFlow(currentPage);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: UiUtils.appBar(context, "流水记录"),
      body: getBody(),
    );
  }

  getBody() {
    if (ps == MyPageState.LOADING) {
      return LoadingPage();
    }

    if (ps == MyPageState.HAVE_DATA) {
      return getContent();
    }
    return Container();
  }

  Widget getContent() {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          UiUtils.divider(),
          Expanded(
              flex: 1,
              child: EasyRefresh.custom(
                controller: _easyRefreshController,
                enableControlFinishLoad: true,
                footer: BallPulseFooter(),
                onLoad: () async {
                  await Future.delayed(Duration(seconds: 1), () {
                    if (currentPage < totlePage) {
                      currentPage++;
                      this.getFlow(currentPage);
                    } else {
                      _easyRefreshController.finishLoad();
                      ToastUtil.showShortToast("没有更多数据了");
                    }
                  });
                },
                slivers: <Widget>[
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (context, index) {
                        return this.getItem(moneyFlowEntity.data.res[index]);
                      },
                      childCount: moneyFlowEntity.data.res.length,
                    ),
                  ),
                ],
              ))
        ],
      ),
    );
  }

  Widget getItem(MoneyFlowDataRe res) {
    return Container(
      margin: EdgeInsets.only(
          top: ScreenUtil().setWidth(30), right: ScreenUtil().setWidth(30)),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Container(
                  padding: EdgeInsets.only(left: ScreenUtil().setWidth(30)),
                  child: Text(
                    "${res.createTime}",
                    style: TextStyles.custom(
                        28, Colours.appC3, Font.PingFangMedium),
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.only(right: ScreenUtil().setWidth(30)),
                child: Text(
                  "${res.memo}",
                  style: TextStyles.custom(
                      28, Colours.appC2, Font.PingFangMedium),
                ),
              ),
              Container(
                child: Text(
                  res.type == "2" ? "+${res.money}金" : "-${res.money}金",
                  style: TextStyles.custom(
                      28,
                      res.type == "2" ? Colours.appC16 : Colours.appC2,
                      Font.PingFangMedium),
                ),
              )
            ],
          ),
          Container(
            margin: EdgeInsets.only(top: ScreenUtil().setWidth(20)),
            child: Row(
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: Container(
                    padding: EdgeInsets.only(left: ScreenUtil().setWidth(30)),
                    child: Text(
                      "${res.remark}",
                      style: TextStyles.custom(
                          28, Colours.appC3, Font.PingFangMedium),
                    ),
                  ),
                ),
                Container(
                  child: Text(
                    "结余:",
                    style: TextStyles.custom(
                        28, Colours.appC2, Font.PingFangMedium),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(20)),
                  child: Text(
                    "${res.income}金",
                    style: TextStyles.custom(
                        28, Colours.appC2, Font.PingFangMedium),
                  ),
                )
              ],
            ),
          ),
          Container(
            child: UiUtils.divider(),
            margin: EdgeInsets.only(top: ScreenUtil().setWidth(30)),
          )
        ],
      ),
    );
  }

  Future getFlow(int current) {
    FormData params =
        FormData.from({"type": "2", "page": "${current}", "size": "10"});
    DioManager.getInstance().get(Api.USER_MONEYLOGLIST, params, (json) {
      MoneyFlowEntity entity = MoneyFlowEntity.fromJson(json);
      _easyRefreshController.finishLoad();
      if (entity == null && moneyFlowEntity == null) {
        ps = MyPageState.NOT_DATA;
        setState(() {});
        return;
      }

      if (current == 1) {
        moneyFlowEntity = null;
      }

      if (entity.code == Api.SUCCESS_CODE) {
        if (null == moneyFlowEntity) {
          moneyFlowEntity = entity;
          totlePage = entity.data.total;
          ps = MyPageState.HAVE_DATA;
          setState(() {});
          return;
        }

        moneyFlowEntity.data.res.addAll(entity.data.res);

        ps = MyPageState.HAVE_DATA;
        setState(() {});
        return;
      }

      if (moneyFlowEntity.code == Api.ERROR_CODE ||
          moneyFlowEntity.code == Api.ERROR_CODE_0) {
        ToastUtil.showShortErrToast(moneyFlowEntity.message);
        return;
      }
    }, (err) {
      ToastUtil.showShortErrToast(Api.ERR_TIME_OUT);
      NavigatorUtils.goBack(context);
    });
  }
}
